import { Body, Controller, Post } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import {
  Crud,
  CrudController,
  CrudRequest,
  ParsedRequest,
  Override,
  QueryOptions,
} from '@crudjs/crud';

import { User } from './user.entity';
import { UsersService } from './users.service';
import { FindManyOptionsReq } from '@crudjs/crud-request/find';
import { findReview } from '@crudjs/crud-typeorm';

const queryOptions: QueryOptions = {
  softDelete: true,
  join: {
    company: {
      exclude: ['description'],
    },
    'company.projects': {
      alias: 'pr',
      exclude: ['description'],
    },
    profile: {
      eager: true,
      exclude: ['updatedAt'],
    },
  },
};
@Crud({
  model: {
    type: User,
  },
  params: {
    companyId: {
      field: 'companyId',
      type: 'number',
    },
    id: {
      field: 'id',
      type: 'number',
      primary: true,
    },
  },
  query: queryOptions,
})
@ApiTags('users')
@Controller('/companies/:companyId/users')
export class UsersController implements CrudController<User> {
  constructor(public service: UsersService) {}

  get base(): CrudController<User> {
    return this;
  }
  @Post('find')
  find(@Body() find: FindManyOptionsReq): any {
    return this.service.find(findReview(find, { query: queryOptions }));
  }

  @Override('getManyBase')
  getAll(@ParsedRequest() req: CrudRequest) {
    return this.base.getManyBase(req);
  }
}
